﻿$(function(){
	$("#member,#logout").hide();
	if($.cookie("user")){
		$("#member,#logout").show();
		$("#reg_a,#login_a").hide();
		$("#member").html($.cookie("user"));
	}else{
		$("#member ,#logout").hide();
		$("#reg_a, #login_a").show();
	}
	
	$("#logout").click(function(){
		$.removeCookie("user");
		window.location.href = "/new/";
	});
	$("#loading").dialog({
		modal : true,
		autoOpen: false,
		closeOnEscape : false,
		resizable : false,
		draggable : false,
		width : 180,
		height: 50,
	}).parent().parent().find(".ui-widget-header").hide();
	// $("#reg_a").click(function(){           /* 这句不能直接写，否则单选按钮在未点击前会出现页面中，而且*/
		$("#reg").dialog({
			title:"后台注册",                  /*下面那句页表中的按钮禁用也会无效*/
			buttons:{
				"提交":function(){
					$(this).submit();
				},
				"取消":function(){
					$(this).dialog("close");
				}
			},
			width:320,
			height:400,
			modal:true,
			autoOpen:false,
			resizable:false,
			closeText:"关闭"
		}).buttonset().validate({
			
			submitHandler : function(form){
			$(form).ajaxSubmit({
				url : "add.php",
				type : "post",
				beforeSubmit : function(formData,jqForm,options){
					$("#loading").dialog("open");
					$("#reg").dialog("widget").find("button").eq(1).button("disable");
				},
				success:function(responseText,statusText){
					if(responseText){
						$("#reg").dialog("widget").find("button").eq(1).button("enable");
						$("#loading").css("background","url(./imgs/success.gif) no-repeat 20px center").html("注册成功");
						setTimeout(function(){
							$("#loading").dialog("close");
							$("#reg").dialog("close");
							$("#reg").resetForm();
							$("#reg span.star").html("*").removeClass("succ");
							$("#loading").css("background","url(./imgs/loading.gif) no-repeat 20px center").html("数据交互中...");
							$("#member,#logout").show();
							$("#reg_a,#login_a").hide();
						},1000);
						location.href="admin.html";
					}
				},
			});
		},
	
	
			showErrors : function(errorMap,errorList){
				var errors = this.numberOfInvalids();
				if(errors > 0){
					$("#reg").dialog("option","height",errors * 20 + 400);
				}else{
					$("#reg").dialog("option","height", 400);
				}
				this.defaultShowErrors();
			},
			
			highlight : function(element,errorClass){
				$(element).css("border","1px solid #630");
				$(element).parent().find("span").html("*").removeClass("succ");
			},
			unhighlight : function(element,errorClass){
				$(element).css("border","1px solid #ccc");
				$(element).parent().find("span").html("&nbsp").addClass("succ");
			},
			errorLabelContainer : "ul.error",
			wrapper :"li",
			rules : {
				user :{
					required :true,
					minlength:2,
					remote:{
						url:"is_user.php",
						type:"POST",
					},
				},
				pass :{
					required :true,
					minlength :6,
				},
				email :{
					required :true,
					email : true,
				},
			},
			messages :{
				user :{
					required : "账号不能为空",
					minlength : jQuery.format("账号不得小于{0}位！"),
					remote:"账号被占用",
				},
				pass :{
					required : "密码不能为空",
					minlength : jQuery.format("密码不得小于{0}位！")
				},
				email :{
					required : "邮箱不得为空",
					minlength : "请输入正确的邮箱",
				},
			},
		});

	
	
	$("#reg_a").click(function(){
		$("#reg").dialog("open");
		$("#reg #user").tooltip("close");
	});
	
	//$('#reg').parent().find('button').eq(1).button('disable');
	
	
	
	$("#birth").datepicker({
		dateFormat:"yy-mm-dd",
		dayNamesMin:["日","一","二","三","四","五","六"],
		monthNames:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		firstDay:"1",
		prevText:"上一月",
		nextText:"下一月",
		yearSuffix:"年",
		showMonthAfterYear:true,
	})
	

	
	$("#email").autocomplete({
		delay:0,
		autoFocus:true,
		source:function(request,response){
			var hosts = ["qq.com","163.com","263.com","sina.com.cn","gmail.com"],
				term = request.term,
				ix = term.indexOf("@"),
				name = term,
				host = "",
				result = [];
			result.push(term);
		    if(ix>-1){
				name = term.slice(0,ix);
				host = term.slice(ix+1);
			}
			if(name){
				var findedHosts = (host?$.grep(hosts,function(value,index){
					return value.indexOf(host)>-1;
				}):hosts);
				findedResults = $.map(findedHosts,function(value,index){
					return name +"@" + value;
				});
			    result = result.concat(findedResults);
			}
			response(result);
		},
	});
	
	$("#login").dialog({
			title:"后台登录",                  /*下面那句页表中的按钮禁用也会无效*/
			buttons:{
				"登录":function(){
					$(this).submit();
				},
				"取消":function(){
					$(this).dialog("close");
				}
			},
			width:320,
			height:280,
			modal:true,
			autoOpen:false,
			resizable:false,
			closeText:"关闭"
		}).validate({
			
			submitHandler : function(form){
			$(form).ajaxSubmit({
				url : "login.php",
				type : "POST",
				beforeSubmit : function(formData,jqForm,options){
					$("#loading").dialog("open");
					$("#login").dialog("widget").find("button").eq(1).button("disable");
				},
				success:function(responseText,statusText){
					if(responseText){
						$("#login").dialog("widget").find("button").eq(1).button("enable");
						$("#loading").css("background","url(./imgs/success.gif) no-repeat 20px center").html("登录成功...");
						setTimeout(function(){
							$("#loading").dialog("close");
							$("#login").dialog("close");
							$("#login").resetForm();
							$("#login span.star").html("*").removeClass("succ");
							$("#loading").css("background","url(./imgs/loading.gif) no-repeat 20px center").html("正在登录中...");
							$("#member,#logout").show();
							$("#reg_a,#login_a").hide();
						},1000);
						location.href="admin.html";
					}
					
				},
			});
		},
	
	
			showErrors : function(errorMap,errorList){
				var errors = this.numberOfInvalids();
				if(errors > 0){
					$("#login").dialog("option","height",errors * 20 + 280);
				}else{
					$("#login").dialog("option","height", 280);
				}
				this.defaultShowErrors();
			},
			
			highlight : function(element,errorClass){
				$(element).css("border","1px solid #630");
				$(element).parent().find("span").html("*").removeClass("succ");
			},
			unhighlight : function(element,errorClass){
				$(element).css("border","1px solid #ccc");
				$(element).parent().find("span").html("&nbsp").addClass("succ");
			},
			errorLabelContainer : "ul.login_error",
			wrapper :"li",
			rules : {
				login_user :{
					required :true,
					minlength:2,
				},
				login_pass :{
					required :true,
					minlength :6,
					remote : {
						url:"login.php",
						type:"POST",
						data:{
							login_user:function(){
								return $("#login_user").val();
							}
						},
					},
				},
			},
			messages :{
				login_user :{
					required : "账号不能为空",
					minlength : jQuery.format("账号不得小于{0}位！"),
				},
				login_pass :{
					required : "密码不能为空",
					minlength : jQuery.format("账号不得小于{0}位！"),
					remote : "账号或密码错误",
				},
			},
		});
	$("#login_a").click(function(){
		$("#login").dialog("open");
	});
	
	
});

























